import { ILoading } from "../../modules_common/loading/ILoading";
import { LoadingConfigDep } from "../../modules_common/loading/LoadingConfigDep";
import { LoadingOne } from "../../modules_common/loading/LoadingOne";
import { LoadingPublicDep } from "../../modules_common/loading/LoadingPublicDep";
import { App } from "../App";
import { GameLogic } from "./GameLogic";


export class LoadingMgr {

    constructor(
        public gameLogic: GameLogic
    ) {

    }

    private _arr: ILoading[] = null;

    // private _arrAll: ILoading[] = null;

    private _allCompleted: boolean = null;
    get allCompleted(): boolean {
        return this._allCompleted;
    }


    init() {
        /**
         * load bundles 加载bundle 
         * 
         * load public 等bundle加载完 加载public
         * 
         * load configs 可以同时加载configs
         */        

        const loadingBundle = new LoadingOne(App.bundleMgr);

        const loadingPublic = new LoadingOne(new LoadingPublicDep(App.bundleMgr));
        loadingBundle.addNext(loadingPublic);

        const loadingConfig = new LoadingOne(new LoadingConfigDep(App.bundleMgr, App.configMgr));
        loadingBundle.addNext(loadingConfig);

        this._arr = [loadingBundle, /*loadingConfig*/];

    }
    finit() {

    }

    startLoading(): void {

        App.timerMgr.setInterval(this.update, 0.02, this);

        const arr = this._arr;

        for (const loading of arr) {
            if (!loading) {
                continue;
            }
            loading.dep.load();
        }

    }

    update(dt: number) {
        let allCompleted = true;
        for (const loading of this._arr) {
            const completed = loading.dep.checkCompleted();
            if (completed) {
                const ret = this.checkNext(loading);
                if (!ret) {
                    allCompleted = false;
                }
            } else {
                allCompleted = false;
            }
        }
        if (allCompleted) {
            App.timerMgr.clearInterval(this.update, this);
            this.gameLogic.onLoadingMgrCompleted();
        }
        this._allCompleted = allCompleted;
    }

    private checkNext(loading: ILoading): boolean {
        let ret = true;
        if (loading.next && loading.next.length) {
            loading.next.forEach(element => {
                element.dep.load();
                if (element.dep.checkCompleted()) {
                    const nextRet = this.checkNext(element);
                    if (!nextRet) {
                        ret = false;
                    }
                } else {
                    ret = false;
                }
            });
        }
        return ret;
    }

    getDesc(): string {
        if (this._allCompleted) {
            return "正在进入游戏..."
        }

        for (const loading of this._arr) {
            const completed = loading.dep.checkCompleted();
            if (completed) {
                const ret = this.getProgressDesc(loading);
                if (ret) {
                    return ret;
                }
            } else {
                return loading.dep.getProgressDesc();
            }
        }

        return ""
    }

    getProgressDesc(loading: ILoading): string {
        if (loading.next && loading.next.length) {
            const next = loading.next;
            for (const nextLoading of next) {
                if (nextLoading.dep.checkCompleted()) {
                    return this.getProgressDesc(nextLoading);
                } else {
                    return nextLoading.dep.getProgressDesc();
                }
            }
        }
        return ""
    }

}